package com.example.liwushuo;

import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ImageView.ScaleType;

public class WelcomeActivity extends Activity {

	private ViewPager viewPager;
	private List<ImageView> list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_welcome);
		viewPager = (ViewPager) findViewById(R.id.welcome_viewPager);
		initAdapter();

		viewPager.setAdapter(new PagerAdapter() {

			@Override
			public Object instantiateItem(ViewGroup container, int position) {
				container.addView(list.get(position));
				return list.get(position);
			}

			@Override
			public boolean isViewFromObject(View arg0, Object arg1) {
				return arg0 == arg1;
			}

			@Override
			public int getCount() {
				return list.size();
			}

			@Override
			public void destroyItem(ViewGroup container, int position,
					Object object) {
				container.removeView(list.get(position));
			}
		});

	}

	private void initAdapter() {

		final boolean netIsConnected = BaseApp
				.isNetworkConnected(getApplicationContext());

		list = new ArrayList<ImageView>();
		ImageView image = null;

		for (int i = 1; i < 5; i++) {
			image = new ImageView(getApplicationContext());
			image.setScaleType(ScaleType.FIT_XY);
			int ImageId = getResources().getIdentifier("walkthrough_" + i,
					"drawable", getPackageName());
			image.setImageResource(ImageId);
			list.add(image);
			
			if (i == 4) {

				image.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						if (netIsConnected) {
							Intent intent = new Intent(getApplicationContext(),
									SelectActivity.class);
							startActivity(intent);
							//
							finish();
						} else {
							Toast.makeText(getApplicationContext(), "网络连接错误", 1)
									.show();
						}
					}
				});
			}
		}
	}
}
